package com.yungu.swift.admin.route.operation;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.admin.utils.WorkbookUtil;
import com.yungu.swift.assets.passenger.model.dto.PassengerCouponBatchLogDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerCouponDto;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerCouponLogPageParam;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerCouponPageParam;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerUuidPageParam;
import com.yungu.swift.assets.passenger.model.vo.PassengerCouponExportVo;
import com.yungu.swift.assets.passenger.service.PassengerCouponBatchLogService;
import com.yungu.swift.assets.passenger.service.PassengerCouponService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @author : cuixiuyin
 * @date : 2019/6/21
 */
@RestController
@RequestMapping("/admin/v1/operation/passenger/coupon")
@Api(value = "【运营管理】", tags = "乘客营销接口")
public class PassengerCouponAdmin {

    @Reference
    private PassengerCouponService passengerCouponService;

    @Reference
    private PassengerCouponBatchLogService passengerCouponBatchLogService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "获取乘客的优惠券的列表", httpMethod = "POST")
    public ResponseData<PageVo<PassengerCouponDto>> queryPage(@RequestBody AdminPassengerUuidPageParam passengerUuidPageParam) {
        if (ParamUtil.isIllegal(passengerUuidPageParam.getPassengerUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return passengerCouponService.queryPage(passengerUuidPageParam);
    }

    @RequestMapping(value = "/queryOrderPage", method = RequestMethod.POST)
    @ApiOperation(value = " 获取乘客的优惠券的列表, 同时关联订单信息", httpMethod = "POST")
    public ResponseData<PageVo<PassengerCouponDto>> queryOrderPage(@RequestBody AdminPassengerCouponPageParam couponPageParam) {
        if (ParamUtil.isIllegal(couponPageParam.getGiftUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return passengerCouponService.queryOrderPage(couponPageParam);
    }

    @RequestMapping(value = "/batchLogPage", method = RequestMethod.POST)
    @ApiOperation(value = " 获取乘客的优惠券发放记录", httpMethod = "POST")
    public ResponseData<PageVo<PassengerCouponBatchLogDto>> batchLogPage(@RequestBody AdminPassengerCouponLogPageParam
                                                                                 couponLogPageParam) {
        return passengerCouponBatchLogService.queryPage(couponLogPageParam);
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation(value = "乘客优惠券关联订单信息导出", httpMethod = "POST")
    public void export(@RequestBody AdminPassengerCouponPageParam couponPageParam, HttpServletResponse response) throws IOException {
        try (OutputStream os = response.getOutputStream()) {
            List<PassengerCouponExportVo> result = passengerCouponService.listExport(couponPageParam).getData();
            InputStream in = getClass().getResourceAsStream("/template/giftDetailTemplate.xlsx");
            Workbook workbook = new XSSFWorkbook(in);
            // 获取统计人数导入格式面板
            Sheet sheet = workbook.getSheetAt(0);
            WorkbookUtil.writeDataToExcelBySheet(sheet, result, PassengerCouponExportVo.class);

            response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode("giftDetail.xlsx", "UTF-8"));
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            workbook.write(os);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
